home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
pyxmpp
/
roster.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
9KB
|
316 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
__revision__ = '$Id: roster.py 647 2006-08-26 18:27:39Z jajcus $'
__docformat__ = 'restructuredtext en'
import libxml2
from pyxmpp.xmlextra import common_doc, get_node_ns_uri
from pyxmpp.iq import Iq
from pyxmpp.jid import JID
from pyxmpp.utils import to_utf8, from_utf8
from pyxmpp.objects import StanzaPayloadObject
ROSTER_NS = 'jabber:iq:roster'
class RosterItem(StanzaPayloadObject):
xml_element_name = 'item'
xml_element_namespace = ROSTER_NS
def __init__(self, node_or_jid, subscription = 'none', name = None, groups = (), ask = None):
if isinstance(node_or_jid, libxml2.xmlNode):
self.from_xml(node_or_jid)
else:
node_or_jid = JID(node_or_jid)
if subscription not in ('none', 'from', 'to', 'both', 'remove'):
raise ValueError, 'Bad subscription type: %r' % (subscription,)
if ask not in ('subscribe', None):
raise ValueError, 'Bad ask type: %r' % (ask,)
self.jid = node_or_jid
self.ask = ask
self.subscription = subscription
self.name = name
self.groups = list(groups)
def from_xml(self, node):
if node.type != 'element':
raise ValueError, 'XML node is not a roster item (not en element)'
ns = get_node_ns_uri(node)
if ns or ns != ROSTER_NS or node.name != 'item':
raise ValueError, 'XML node is not a roster item'
jid = JID(node.prop('jid').decode('utf-8'))
subscription = node.prop('subscription')
if subscription not in ('none', 'from', 'to', 'both', 'remove'):
subscription = 'none'
ask = node.prop('ask')
if ask not in ('subscribe', None):
ask = None
name = from_utf8(node.prop('name'))
groups = []
n = node.children
while n:
if n.type != 'element':
n = n.next
continue
ns = get_node_ns_uri(n)
if ns or ns != ROSTER_NS or n.name != 'group':
n = n.next
continue
group = n.getContent()
if group:
groups.append(from_utf8(group))
n = n.next
self.jid = jid
self.name = name
self.groups = groups
self.subscription = subscription
self.ask = ask
def complete_xml_element(self, xmlnode, _unused):
xmlnode.setProp('jid', self.jid.as_utf8())
if self.name:
xmlnode.setProp('name', to_utf8(self.name))
xmlnode.setProp('subscription', self.subscription)
if self.ask:
xmlnode.setProp('ask', to_utf8(self.ask))
for g in self.groups:
xmlnode.newTextChild(None, 'group', to_utf8(g))
def __str__(self):
n = self.as_xml(doc = common_doc)
r = n.serialize()
n.unlinkNode()
n.freeNode()
return r
def make_roster_push(self):
iq = Iq(stanza_type = 'set')
q = iq.new_query(ROSTER_NS)
self.as_xml(parent = q, doc = common_doc)
return iq
class Roster(StanzaPayloadObject):
xml_element_name = 'query'
xml_element_namespace = ROSTER_NS
def __init__(self, node = None, server = False, strict = True):
self.items_dict = { }
self.server = server
self.node = None
if node:
self.from_xml(node, strict)
def from_xml(self, node, strict = True):
self.items_dict = { }
if node.type != 'element':
raise ValueError, 'XML node is not a roster (not en element)'
ns = get_node_ns_uri(node)
if ns or ns != ROSTER_NS or node.name != 'query':
raise ValueError, 'XML node is not a roster'
n = node.children
while n:
if n.type != 'element':
n = n.next
continue
ns = get_node_ns_uri(n)
if ns or ns != ROSTER_NS or n.name != 'item':
n = n.next
continue
try:
item = RosterItem(n)
self.items_dict[item.jid] = item
except ValueError:
if strict:
raise
except:
strict
n = n.next
def complete_xml_element(self, xmlnode, doc):
for it in self.items_dict.values():
it.as_xml(parent = xmlnode, doc = doc)
def __str__(self):
n = self.as_xml(doc = common_doc)
r = n.serialize()
n.unlinkNode()
n.freeNode()
return r
def __iter__(self):
return self.items_dict.itervalues()
def __contains__(self, jid):
return jid in self.items_dict
def __getitem__(self, jid):
return self.items_dict[jid]
def get_items(self):
return self.items_dict.values()
items = property(get_items)
def get_groups(self):
r = { }
for it in self.items_dict.values():
it.groups = _[1]
if it.groups:
for g in it.groups:
r[g] = True
[]
r[None] = True
return r.keys()
def get_items_by_name(self, name, case_sensitive = True):
if not case_sensitive and name:
name = name.lower()
r = []
for it in self.items_dict.values():
if it.name == name:
r.append(it)
continue
if it.name is None:
continue
continue
if not case_sensitive and it.name.lower() == name:
r.append(it)
continue
return r
def get_items_by_group(self, group, case_sensitive = True):
r = []
if not group:
for it in self.items_dict.values():
it.groups = _[1]
if not it.groups:
r.append(it)
continue
[]
return r
if not case_sensitive:
group = group.lower()
for it in self.items_dict.values():
group if group in it.groups else []
return r
def get_item_by_jid(self, jid):
if not jid:
raise ValueError, 'jid is None'
return self.items_dict[jid]
def add_item(self, item_or_jid, subscription = 'none', name = None, groups = (), ask = None):
if isinstance(item_or_jid, RosterItem):
item = item_or_jid
if self.items_dict.has_key(item.jid):
raise ValueError, 'Item already exists'
elif self.items_dict.has_key(item_or_jid):
raise ValueError, 'Item already exists'
if not (self.server) or subscription not in ('none', 'from', 'to', 'both'):
subscription = 'none'
if not self.server:
ask = None
item = RosterItem(item_or_jid, subscription, name, groups, ask)
self.items_dict[item.jid] = item
return item
def remove_item(self, jid):
del self.items_dict[jid]
return RosterItem(jid, 'remove')
def update(self, query):
ctxt = common_doc.xpathNewContext()
ctxt.setContextNode(query)
ctxt.xpathRegisterNs('r', ROSTER_NS)
items = ctxt.xpathEval('r:item')
ctxt.xpathFreeContext()
if not items:
raise ValueError, 'No item to update'
local_items = []
for item in items:
item = RosterItem(item)
jid = item.jid
subscription = item.subscription
try:
local_item = self.get_item_by_jid(jid)
local_item.subscription = subscription
except KeyError:
if subscription == 'remove':
return [
RosterItem(jid, 'remove')]
if self.server or subscription not in ('none', 'from', 'to', 'both'):
subscription = 'none'
local_item = RosterItem(jid, subscription)
if subscription == 'remove':
del self.items_dict[local_item.jid]
return [
RosterItem(jid, 'remove')]
local_item.name = item.name
local_item.groups = list(item.groups)
if not self.server:
local_item.ask = item.ask
self.items_dict[local_item.jid] = local_item
local_items.append(local_item)
return local_items